class ChessPiece:
    def __init__(self, color, row, col):
        self.color = color  # 'white' 或 'black'
        self.row = row
        self.col = col
        self.has_moved = False
        self.type = None  # 在子类中设置
    
    def get_moves(self, board):
        # 在子类中实现
        pass
    
    def __str__(self):
        return f"{self.color}_{self.type} at ({self.row}, {self.col})"


class Pawn(ChessPiece):
    def __init__(self, color, row, col):
        super().__init__(color, row, col)
        self.type = 'pawn'
    
    def get_moves(self, board):
        moves = []
        direction = -1 if self.color == 'white' else 1  # 白色向上(行减小)，黑色向下(行增加)
        
        # 向前移动一格
        new_row = self.row + direction
        if 0 <= new_row < 8 and board.board[new_row][self.col] is None:
            moves.append((new_row, self.col))
            
            # 如果是第一次移动，可以向前移动两格
            if ((self.color == 'white' and self.row == 6) or 
                (self.color == 'black' and self.row == 1)):
                new_row = self.row + 2 * direction
                if 0 <= new_row < 8 and board.board[new_row][self.col] is None:
                    moves.append((new_row, self.col))
        
        # 斜向吃子
        for col_offset in [-1, 1]:
            new_col = self.col + col_offset
            new_row = self.row + direction
            
            if 0 <= new_row < 8 and 0 <= new_col < 8:
                target_piece = board.board[new_row][new_col]
                if target_piece and target_piece.color != self.color:
                    moves.append((new_row, new_col))
        
        # TODO: 可以添加吃过路兵规则
        
        return moves


class Rook(ChessPiece):
    def __init__(self, color, row, col):
        super().__init__(color, row, col)
        self.type = 'rook'
    
    def get_moves(self, board):
        moves = []
        
        # 上下左右四个方向
        directions = [(-1, 0), (1, 0), (0, -1), (0, 1)]
        
        for direction in directions:
            for step in range(1, 8):
                new_row = self.row + direction[0] * step
                new_col = self.col + direction[1] * step
                
                # 检查是否在棋盘内
                if not (0 <= new_row < 8 and 0 <= new_col < 8):
                    break
                
                target_piece = board.board[new_row][new_col]
                
                if target_piece is None:
                    # 空位可以移动
                    moves.append((new_row, new_col))
                elif target_piece.color != self.color:
                    # 可以吃掉对方棋子，然后停止
                    moves.append((new_row, new_col))
                    break
                else:
                    # 遇到自己的棋子，停止
                    break
        
        return moves


class Knight(ChessPiece):
    def __init__(self, color, row, col):
        super().__init__(color, row, col)
        self.type = 'knight'
    
    def get_moves(self, board):
        moves = []
        
        # 马走"日"，八个可能的位置
        knight_moves = [
            (-2, -1), (-2, 1), (-1, -2), (-1, 2),
            (1, -2), (1, 2), (2, -1), (2, 1)
        ]
        
        for move in knight_moves:
            new_row = self.row + move[0]
            new_col = self.col + move[1]
            
            # 检查是否在棋盘内
            if not (0 <= new_row < 8 and 0 <= new_col < 8):
                continue
            
            target_piece = board.board[new_row][new_col]
            
            if target_piece is None or target_piece.color != self.color:
                # 空位或者可以吃掉对方棋子
                moves.append((new_row, new_col))
        
        return moves


class Bishop(ChessPiece):
    def __init__(self, color, row, col):
        super().__init__(color, row, col)
        self.type = 'bishop'
    
    def get_moves(self, board):
        moves = []
        
        # 四个对角线方向
        directions = [(-1, -1), (-1, 1), (1, -1), (1, 1)]
        
        for direction in directions:
            for step in range(1, 8):
                new_row = self.row + direction[0] * step
                new_col = self.col + direction[1] * step
                
                # 检查是否在棋盘内
                if not (0 <= new_row < 8 and 0 <= new_col < 8):
                    break
                
                target_piece = board.board[new_row][new_col]
                
                if target_piece is None:
                    # 空位可以移动
                    moves.append((new_row, new_col))
                elif target_piece.color != self.color:
                    # 可以吃掉对方棋子，然后停止
                    moves.append((new_row, new_col))
                    break
                else:
                    # 遇到自己的棋子，停止
                    break
        
        return moves


class Queen(ChessPiece):
    def __init__(self, color, row, col):
        super().__init__(color, row, col)
        self.type = 'queen'
    
    def get_moves(self, board):
        moves = []
        
        # 横、直、斜八个方向
        directions = [
            (-1, -1), (-1, 0), (-1, 1),
            (0, -1), (0, 1),
            (1, -1), (1, 0), (1, 1)
        ]
        
        for direction in directions:
            for step in range(1, 8):
                new_row = self.row + direction[0] * step
                new_col = self.col + direction[1] * step
                
                # 检查是否在棋盘内
                if not (0 <= new_row < 8 and 0 <= new_col < 8):
                    break
                
                target_piece = board.board[new_row][new_col]
                
                if target_piece is None:
                    # 空位可以移动
                    moves.append((new_row, new_col))
                elif target_piece.color != self.color:
                    # 可以吃掉对方棋子，然后停止
                    moves.append((new_row, new_col))
                    break
                else:
                    # 遇到自己的棋子，停止
                    break
        
        return moves


class King(ChessPiece):
    def __init__(self, color, row, col):
        super().__init__(color, row, col)
        self.type = 'king'
    
    def get_moves(self, board):
        moves = []
        
        # 八个方向都可以走一步
        directions = [
            (-1, -1), (-1, 0), (-1, 1),
            (0, -1), (0, 1),
            (1, -1), (1, 0), (1, 1)
        ]
        
        for direction in directions:
            new_row = self.row + direction[0]
            new_col = self.col + direction[1]
            
            # 检查是否在棋盘内
            if not (0 <= new_row < 8 and 0 <= new_col < 8):
                continue
            
            target_piece = board.board[new_row][new_col]
            
            if target_piece is None or target_piece.color != self.color:
                # 空位或者可以吃掉对方棋子
                moves.append((new_row, new_col))
        
        # 王车易位规则在ChessBoard的get_valid_moves中实现
        
        return moves 